#include "DHD_MessageSystem.h"

DHD_MessageSystem* DHD_MessageSystem::GetInstance(void)
{
	static DHD_MessageSystem instance;
	return &instance;
}


//	Setup the function pointer for our messages.
void DHD_MessageSystem::InitMessageSystem(MESSAGEPROC MsgProc)
{
	if(!MsgProc)
		return;

	m_pMsgProc = MsgProc;
}

//	Sends the message into the queue and awaits processing later on through
//	The ProcessMessages() function.
void DHD_MessageSystem::SendMsg(CBaseMessage* pMsg)
{
	if(!pMsg)
		return;

	m_MsgQueue.push(pMsg);
}

//	Processes all the messages that are waiting inside of the queue.
//	Normally you only call this function once per frame.
void DHD_MessageSystem::ProcessMessages(void)
{
	if(!m_pMsgProc)
		return;

	while(!m_MsgQueue.empty())
	{
		m_pMsgProc(m_MsgQueue.front());
		delete m_MsgQueue.front();
		m_MsgQueue.pop();
	}
}


//	Clears any messages that may be remaining
void DHD_MessageSystem::ClearMessages(void)
{
	while(!m_MsgQueue.empty())
	{
		delete m_MsgQueue.front();
		m_MsgQueue.pop();
	}
}


//	Clears any messages that may be remaining and sets the function pointer to NULL.
void DHD_MessageSystem::ShutdownMessageSystem(void)
{
	ClearMessages();

	m_pMsgProc = NULL;
}

